{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"faceId\": \"f8ed7175-60b6-461a-95b3-15c3e5fe5516\", \"faceRectangle\": {\"top\": 417, \"left\": 404, \"width\": 307, \"height\": 307}, \"faceAttributes\": {\"smile\": 0.735, \"headPose\": {\"pitch\": -6.6, \"roll\": -23.8, \"yaw\": -54.6}, \"gender\": \"male\", \"age\": 21.0, \"facialHair\": {\"moustache\": 0.1, \"beard\": 0.1, \"sideburns\": 0.1}, \"glasses\": \"NoGlasses\", \"emotion\": {\"anger\": 0.0, \"contempt\": 0.0, \"disgust\": 0.0, \"fear\": 0.0, \"happiness\": 0.735, \"neutral\": 0.0, \"sadness\": 0.264, \"surprise\": 0.0}, \"blur\": {\"blurLevel\": \"low\", \"value\": 0.15}, \"exposure\": {\"exposureLevel\": \"overExposure\", \"value\": 0.88}, \"noise\": {\"noiseLevel\": \"high\", \"value\": 0.72}, \"makeup\": {\"eyeMakeup\": false, \"lipMakeup\": true}, \"accessories\": [], \"occlusion\": {\"foreheadOccluded\": false, \"eyeOccluded\": false, \"mouthOccluded\": false}, \"hair\": {\"bald\": 0.06, \"invisible\": false, \"hairColor\": [{\"color\": \"black\", \"confidence\": 1.0}, {\"color\": \"brown\", \"confidence\": 0.85}, {\"color\": \"other\", \"confidence\": 0.51}, {\"color\": \"gray\", \"confidence\": 0.2}, {\"color\": \"red\", \"confidence\": 0.07}, {\"color\": \"blond\", \"confidence\": 0.02}, {\"color\": \"white\", \"confidence\": 0.0}]}}}]'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-1 面部检测\n",
    "import requests\n",
    "# json\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = \"c4c5ef3d9a0c41b4b93f606d1e5fddfe\"\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://qdtapi.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'https://gitee.com/li_man_qi/api_start/raw/master/images/dl.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "json.dumps(response.json())\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bytes or string   编码不同  *****  encoding =\"utf-8\"\n",
    "# response.text\n",
    "# response.content\n",
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '1b0e7225-3c37-440d-95e2-5fc9ec413d51',\n",
       "  'faceRectangle': {'top': 417, 'left': 404, 'width': 307, 'height': 307},\n",
       "  'faceAttributes': {'smile': 0.735,\n",
       "   'headPose': {'pitch': -6.6, 'roll': -23.8, 'yaw': -54.6},\n",
       "   'gender': 'male',\n",
       "   'age': 21.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.735,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.264,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.15},\n",
       "   'exposure': {'exposureLevel': 'overExposure', 'value': 0.88},\n",
       "   'noise': {'noiseLevel': 'high', 'value': 0.72},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.06,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'brown', 'confidence': 0.85},\n",
       "     {'color': 'other', 'confidence': 0.51},\n",
       "     {'color': 'gray', 'confidence': 0.2},\n",
       "     {'color': 'red', 'confidence': 0.07},\n",
       "     {'color': 'blond', 'confidence': 0.02},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '1b0e7225-3c37-440d-95e2-5fc9ec413d51',\n",
       "  'faceRectangle': {'top': 417, 'left': 404, 'width': 307, 'height': 307},\n",
       "  'faceAttributes': {'smile': 0.735,\n",
       "   'headPose': {'pitch': -6.6, 'roll': -23.8, 'yaw': -54.6},\n",
       "   'gender': 'male',\n",
       "   'age': 21.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.735,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.264,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.15},\n",
       "   'exposure': {'exposureLevel': 'overExposure', 'value': 0.88},\n",
       "   'noise': {'noiseLevel': 'high', 'value': 0.72},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.06,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'brown', 'confidence': 0.85},\n",
       "     {'color': 'other', 'confidence': 0.51},\n",
       "     {'color': 'gray', 'confidence': 0.2},\n",
       "     {'color': 'red', 'confidence': 0.07},\n",
       "     {'color': 'blond', 'confidence': 0.02},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-2\n",
    "results = response.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "faceListId = \"supermonkey02\" #学生填写\n",
    "#create_facelists_url = 'https://qdtapi.cognitiveservices.azure.com/face/v1.0/facelists/{}' #终结点\n",
    "create_facelists_url = 'https://qdtapi.cognitiveservices.azure.com/face/v1.0/facelists/{}'\n",
    "subscription_key = '559a9e5163c949968dbc907fb2820b28'#学生填写\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "data = {\n",
    "        \"name\": \"plgg\",\n",
    "        \"userData\": \"照片集\",\n",
    "        \"recognitionModel\": \"recognition_03\",\n",
    "# 学生填写\n",
    "    \n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data) #学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "2、Add face\n",
    "add_face_url = \"https://qdtapi.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"https://gitee.com/li_man_qi/api_start/raw/master/images/wlh.jpg\" #学生填写\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"王力宏\"\n",
    "#学生填写\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '4bb8ed11-83f5-4d64-a581-fc14f88eebd2'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json()#返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://qdtapi.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://gitee.com/li_man_qi/api_start/raw/master/images/wlh.jpg\",\"王力宏\")\n",
    "AddFace(\"https://gitee.com/li_man_qi/api_start/raw/master/images/lzt.jpg\",\"李治廷\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '4bb8ed11-83f5-4d64-a581-fc14f88eebd2',\n",
       "   'userData': '李治廷'},\n",
       "  {'persistedFaceId': '45aa5644-23fc-44de-b976-dc8d08cb5884',\n",
       "   'userData': '王力宏'},\n",
       "  {'persistedFaceId': 'dced303c-e7dd-440e-8e04-ebbe8dfd016a',\n",
       "   'userData': '王力宏'},\n",
       "  {'persistedFaceId': 'b7263a70-dfdd-4328-91ea-e0a08b7dcf60',\n",
       "   'userData': '李治廷'}],\n",
       " 'faceListId': 'supermonkey02',\n",
       " 'name': 'plgg',\n",
       " 'userData': '照片集'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url =\"https://qdtapi.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers,params=data)#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '4bb8ed11-83f5-4d64-a581-fc14f88eebd2',\n",
       "  'userData': '李治廷'},\n",
       " {'persistedFaceId': '45aa5644-23fc-44de-b976-dc8d08cb5884',\n",
       "  'userData': '王力宏'},\n",
       " {'persistedFaceId': 'dced303c-e7dd-440e-8e04-ebbe8dfd016a',\n",
       "  'userData': '王力宏'},\n",
       " {'persistedFaceId': 'b7263a70-dfdd-4328-91ea-e0a08b7dcf60',\n",
       "  'userData': '李治廷'}]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceID =  r_get_facelist.json()['persistedFaces']\n",
    "faceID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'persistedFaceId': '4bb8ed11-83f5-4d64-a581-fc14f88eebd2', 'userData': '李治廷'}\n",
      "{'persistedFaceId': '45aa5644-23fc-44de-b976-dc8d08cb5884', 'userData': '王力宏'}\n",
      "{'persistedFaceId': 'dced303c-e7dd-440e-8e04-ebbe8dfd016a', 'userData': '王力宏'}\n",
      "{'persistedFaceId': 'b7263a70-dfdd-4328-91ea-e0a08b7dcf60', 'userData': '李治廷'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '4bb8ed11-83f5-4d64-a581-fc14f88eebd2',\n",
       "  'userData': '李治廷'},\n",
       " {'persistedFaceId': '45aa5644-23fc-44de-b976-dc8d08cb5884',\n",
       "  'userData': '王力宏'},\n",
       " {'persistedFaceId': 'dced303c-e7dd-440e-8e04-ebbe8dfd016a',\n",
       "  'userData': '王力宏'},\n",
       " {'persistedFaceId': 'b7263a70-dfdd-4328-91ea-e0a08b7dcf60',\n",
       "  'userData': '李治廷'}]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 键/值\n",
    "for i in faceID:\n",
    "     print(i)\n",
    "if i[\"userData\"] == '王力宏':\n",
    "        faceID = i['persistedFaceId']\n",
    "faceID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'daf08d77-07a1-4b54-86fd-15c2f6aaf4e7',\n",
       "  'faceRectangle': {'top': 460, 'left': 189, 'width': 141, 'height': 141},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 12.5, 'roll': 13.9, 'yaw': -80.4},\n",
       "   'gender': 'male',\n",
       "   'age': 18.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.986,\n",
       "    'sadness': 0.013,\n",
       "    'surprise': 0.001},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.64},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.43},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.33},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.05,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 1.0},\n",
       "     {'color': 'red', 'confidence': 0.74},\n",
       "     {'color': 'black', 'confidence': 0.36},\n",
       "     {'color': 'blond', 'confidence': 0.25},\n",
       "     {'color': 'other', 'confidence': 0.11},\n",
       "     {'color': 'gray', 'confidence': 0.05},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://qdtapi.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'https://gitee.com/li_man_qi/api_start/raw/master/images/dl2.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://qdtapi.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"daf08d77-07a1-4b54-86fd-15c2f6aaf4e7\",#取上方的faceID\n",
    "    \"faceListId\": \"supermonkey02\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '45aa5644-23fc-44de-b976-dc8d08cb5884',\n",
       "  'confidence': 0.08704},\n",
       " {'persistedFaceId': 'dced303c-e7dd-440e-8e04-ebbe8dfd016a',\n",
       "  'confidence': 0.08704},\n",
       " {'persistedFaceId': '4bb8ed11-83f5-4d64-a581-fc14f88eebd2',\n",
       "  'confidence': 0.08288},\n",
       " {'persistedFaceId': 'b7263a70-dfdd-4328-91ea-e0a08b7dcf60',\n",
       "  'confidence': 0.08288}]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "变成表格遇到困难，不成功T^T"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
